package cn.dm.service.impl;

import cn.dm.client.RestDmAreaClient;
import cn.dm.client.RestDmUserClient;
import cn.dm.common.Dto;
import cn.dm.common.DtoUtil;
import cn.dm.common.EmptyUtils;
import cn.dm.common.MD5;
import cn.dm.pojo.DmUser;
import cn.dm.service.LoginService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class LoginServiceImpl implements LoginService {

    @Resource
    private RestDmAreaClient restDmAreaClient;

    @Resource
    private RestDmUserClient restDmUserClient;

    /**
     * 用户登录请求不带token
     * @param dmUser
     * @return
     * @throws Exception
     */
    @Override
    public Dto login(DmUser dmUser) throws Exception {
        String password = MD5.getMd5(dmUser.getPassword(), 32);
        dmUser.setPassword(password);
        DmUser dmUser1 = restDmUserClient.checkLoginByPassword(dmUser);
        if(EmptyUtils.isNotEmpty(dmUser1)){
            if(dmUser.getPhone().equals(dmUser1.getPhone()) || password.equals(dmUser1.getPassword())){
                return DtoUtil.returnDataSuccess(dmUser1);
            }else{
                return DtoUtil.returnFail("账号或密码错误","1006");
            }
        }else{
            return DtoUtil.returnFail("账号或密码没有输入","1006");
        }

    }
}
